<?php
namespace Admin\Controller\Goods;
use Admin\Controller\Auto\AutoController;
use Admin\Controller\User\PersonalController;
	class GoodsController extends AutoController {
		public function index(){
			$this->display();
		}

		public function upload($rootPath){
			// return $_FILES;
			$upload = new \Think\Upload();
			$upload->maxSize=3145728;
			$upload->exts      = array('jpg', 'gif', 'png', 'jpeg');
			$upload->savePath  =  '/Uploads/'; 
			// 设置附件上传目录  
			$upload->rootPath  = $rootPath ;
			// var_dump($upload);die;
			$info = $upload->upload();
			if(!$info) {// 上传错误提示错误信息  
			    $this->error($upload->getError());  
			}else{// 上传成功 获取上传文件信息  
			    foreach($info as $file){  
					$path = trim($upload->rootPath,'.').$file['savepath'].$file['savename'];
				}
			}
			//time(); ->path  mtime();->图片
			//$_SESSION['']
			return $path;
		}

		public function update(){
			$data['error']=0;
			// $data['pic']='/public/admin/upload/images/1.jpg';
			// $data['files']=$this->upload();
			$data['pic'] = $this->upload('./public/plupload');
			$data['name'] = explode('/',$data['pic'])[5];
			// $data['name'] = '1.jpg';
			$this->ajaxReturn($data);
		}
		public function lunbo(){
			$data['goods_pic'] = $this->upload('./public/plupload');
			$data['gid'] = I('post.gid');
			$data['goods_zpic'] = 2;
			$pic = M('g_goods_pic');
			$res = $pic->add($data);
			if($res){
				echo '<script type="text/javascript">
				parent.lunbo("yes");
				</script>';
			}else{
				echo '<script type="text/javascript">
				parent.lunbo("no");
				</script>';
			}
		}
		public function getlunbo(){
			$data = M('g_goods_pic')->where('goods_zpic=2')->select();
			$this->ajaxReturn($data);
		}
		public function quxiaolunbo(){
			$where['gid']=I('post.gid');
			$where['goods_zpic']=2;
			$pic = M('g_goods_pic')->where($where)->find()['goods_pic'];
			if(file_exists('.'.$pic)){
				unlink('.'.$pic);
			}
			if(M('g_goods_pic')->where($where)->delete()){
				echo 'yes';
			}else{
				echo 'no';
			}
		}
		public function delpic(){
			// var_dump(I('post.pic'));
			if(file_exists('.'.I('post.pic'))){
				unlink('.'.I('post.pic'));
			}
		}
		public function delp(){
			// var_dump(I('post.del'));
			$list = I('post.del');
			foreach($list as $k=>$v){
				if(file_exists('.'.$v)){
					unlink('.'.$v);
				}
			}
		}
		public function insert(){
			// var_dump($_POST);die;
			$data = I('post.data');
			$data['uid']=session('id');
			$data['goods_time']=time();
			$data['goods_num']=0;
			$data['position']=json_encode($data['position']);
			// var_dump($data);die;
			$goods = D('g_goods');
			$goods->create($data);
			$gid = $goods->add();
			if($gid){
				$list = [];
				foreach($data['pic'] as $k => $v){
					$list[$k]['goods_pic']=$v;
					$list[$k]['gid']=$gid;
					if($k==0){
						$list[$k]['goods_zpic']=1;
					}else{
						$list[$k]['goods_zpic']=0;
					}
				}
				if(M('g_goods_pic')->addAll($list)){
					echo 'yes';
				}else{
					$goods->delete($gid);
					echo 'no';
				}
			}else{
				echo 'no';
			}
		}
		public function limit(){
			$where['goods_name'] = array('like',"%".I('get.keyword')."%");
			$count = M('g_goods')->where($where)->count();
			$num = $count/I('get.mylimit');
			$limit = (I('get.ilimit')-1)*I('get.mylimit');
			$data = M('g_goods')
			->alias('a')
			->field(array('a.*','b.type_name','c.username','d.type_name'=>'pinpai'))
			->join('left join think_g_type b on a.tid=b.id')
			->join('left join think_g_type d on a.pinpai=d.id')
			->join('left join think_user c on a.uid=c.id')
			->where($where)
			->limit($limit.','.I('get.mylimit'))
			->select();
			$pic = M('g_goods_pic')->where('goods_zpic=1')->select();
			$type = M('g_type')->where('pid=0')->select();
			foreach($data as $k=>$v){
				$where['gid']=$v['id'];
				$where['goods_zpic']=1;
				$position = json_decode($v['position']);
				$posit = [];
				foreach($type as $keys=>$value){
					foreach($position as $v1){
						if($value['id']==$v1){
							$posit[]=$value['type_name'];
						}
					}
				}

				$data[$k]['position']=$posit;
				$data[$k]['goods_time'] = date('Y-m-d',$v['goods_time']);
				foreach($pic as $kye=>$val){
					if($v['id']==$val['gid']){
						$data[$k]['pic']=$val['goods_pic'];
					}
				}
			}

			$data[count($data)]=ceil($num);
			$this->AjaxReturn($data);
		}
		public function del(){
			$pic = M('g_goods_pic');
			$goods = M('g_goods');
			$goods->startTrans();
			$picPath=$pic->where('gid = '.I('post.id'))->select();
			$res = $goods->delete(I('post.id'));
			$result = $pic->where('gid='.I('post.id'))->delete();
			if($res && $result){
				foreach($picPath as $k=>$v){
					if(file_exists('.'.$v['goods_pic'])){
						unlink('.'.$v['goods_pic']);
					}
				}
				$goods->commit();
				echo 'yes';
			}else{
				$goods->rollback();
				echo 'no';
			}
		}

		public function updategoods(){
			// var_dump(I('post.'));
			$data = I('post.data');
			$data['uid']=session('id');
			$data['goods_time']=time();
			$data['position']=json_encode($data['position']);
			$goods=M('g_goods');
			$goods->create($data);
			if($goods->save()){
				echo 'yes';
			}else{
				echo 'no';
			}
		}

		public function getpic(){
			$where['gid']=['eq',I('post.id')];
			$where['goods_zpic']=['lt',2];
			$pic = M('g_goods_pic')->where($where)->select();
			$this->AjaxReturn($pic);
		}

		public function updatepic(){
			$data['goods_pic'] = $this->upload('./public/plupload');
			$data['id']=I('post.id');
			$pic = M('g_goods_pic');
			$pic->create($data);
			if($pic->save()){
				if(file_exists('.'.I('post.pic1'))){
					unlink('.'.I('post.pic1'));
				}
				echo '<script type="text/javascript">
					//输出父窗口的变量
					parent.uploadpic("yes");
				</script>';
			}else{
				echo '<script type="text/javascript">
					//输出父窗口的变量
					parent.uploadpic("no");
				</script>';
			}
		}
	}
?>